<?php

namespace IDock\Insurance\MACN\Request;

use Exception;
use IDock\Insurance\MACN\MACNEnums;
use IDock\Insurance\MACN\Response\UploadFileResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\UploadFileVisitor;

class UploadFileRequest extends AllMACNRequest
{
    protected $requestType = '0026';

    protected $response = UploadFileResponse::class;

    protected $urlTip = '影像资料上传';

    //protected $inLog = false;

    public $operatorCode;

    /**
     * @var UploadFileVisitor $visitor
     */
    protected $visitor;

    /**
     * file
     * @var array  $file
     */
    public $file;

    /**
     * @return array
     * @throws Exception
     */
    public function generate(): array
    {

        if ($this->visitor instanceof PremiumInquiryVisitor) {
            return [
                'imageUpload' => [
                    'companyCode' => $this->visitor->getExtInfoVisitor()->argc['companyCode'] ?? '',
                    'operatorCode' => $this->visitor->getExtInfoVisitor()->argc['operatorCode'] ?? '',
                    'preProposalTopNo' => $this->visitor->getExtInfoVisitor()->argc['preProposalTopNo'] ?? '',
                    'fileList' => ['fileInfo' => $this->getFileList()],
                ]
            ];
        } else {
            return [
                'imageUpload' => [
                    'companyCode' => $this->visitor->argc['companyCode'] ?? '',
                    'operatorCode' => $this->visitor->argc['operatorCode'] ?? '',
                    'preProposalTopNo' => $this->visitor->argc['preProposalTopNo'] ?? '',
                    'fileList' => ['fileInfo' => $this->getFileList()],
                ]
            ];
        }
    }

    private function getFileList() :array
    {
        $files = [$this->file];
        $imageList = [];
        foreach ($files as $file){
            $extArr = explode('.',$file['url']);
            $ext = end($extArr);
            if($ext == 'jpeg'){
                $ext = 'jpg';
            }
            if(!in_array($ext,['png','jpg'])){
                throw new Exception("仅支持png和jpg");
            }
            $imageList[] = [
                'base64' => base64_encode(file_get_contents($file['url'])),
                'documentType' => MACNEnums::$uploadFileType[$file['type']],
//                'url' => $file['url'],
                'urlType' => $ext,//后缀名
            ];
        }
        return $imageList;
    }

    private function getFileList2()
    {
        $files = $this->visitor->getFileList();
        $imageList = [];
        foreach ($files as $file){
            $extArr = explode('.',$file['url']);
            $ext = end($extArr);
            if($ext == 'jpeg'){
                $ext = 'jpg';
            }
            if(!in_array($ext,['png','jpg'])){
                throw new Exception("仅支持png和jpg");
            }
            $imageList[] = [
                'base64' => base64_encode(file_get_contents($file['url'])),
                'documentType' => MACNEnums::$uploadFileType[$file['type']],
//                'url' => $file['url'],
                'urlType' => $ext,//后缀名
            ];
        }
        return $imageList;
    }
}
